package com.elink.enginerring.dict.entity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.regex.Pattern;

/**
 * 创建word文档 步骤: 1,建立文档 2,创建一个书写器 3,打开文档 4,向文档中写入数据 5,关闭文档
 */
public class Word {
	public static void printWord(String str, String abbreviation,
			String spelling, String translate) {
		// System.err.println("字符串为:"+str);
		System.out.printf("缩写为:%s单词为:%s翻译为:%s", (abbreviation == "" ? "无": abbreviation), spelling, translate);
		System.out.println();
	}

	public static void printError(String str) {
		System.err.println("错误格式:" + str);
	}

	public static boolean isUpperCase(char c) {
		int intForChar = (int) c;
		return (intForChar >= 97 && intForChar <= 122) ? false : true;
	}
	public static boolean isLowerCase(char c) {
		int intForChar = (int) c;
		return (intForChar >= 97 && intForChar <= 122) ? true : false;
	}
	
	public static boolean isUpperString (String s) {
		for (int  i = 0;i  < s.length(); i++) {
			if (isLowerCase(s.charAt(i))) {
				return false;
			}
		}
		return true;
	}
	public static boolean isLowerString (String s) {
		for (int  i = 0;i  < s.length(); i++) {
			if (isUpperCase(s.charAt(i))) {
				return false;
			}
		}
		return true;
	}
	public static boolean isChineseWord(String[] strArr) {
		for (int i = strArr.length - 1; i >= 0; i--) {
			if (isChineseByName(strArr[i])) {
				return true;
			}
		}
		return false;
	}

	// 根据Unicode编码完美的判断中文汉字和符号
	private static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
			return true;
		}
		return false;
	}

	// 完整的判断中文汉字和符号
	public static boolean isChinese(String strName) {
		char[] ch = strName.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			if (isChinese(c)) {
				return true;
			}
		}
		return false;
	}

	// 只能判断部分CJK字符（CJK统一汉字）
	public static boolean isChineseByREG(String str) {
		if (str == null) {
			return false;
		}
		Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+");
		return pattern.matcher(str.trim()).find();
	}

	// 只能判断部分CJK字符（CJK统一汉字）
	public static boolean isChineseByName(String str) {
		if (str == null) {
			return false;
		}
		// 大小写不同：\\p 表示包含，\\P 表示不包含
		// \\p{Cn} 的意思为 Unicode 中未被定义字符的编码，\\P{Cn} 就表示 Unicode中已经被定义字符的编码
		String reg = "\\p{InCJK Unified Ideographs}&&\\P{Cn}";
		Pattern pattern = Pattern.compile(reg);
		return pattern.matcher(str.trim()).find();
	}

	public static int getAbbreviationIndexOf(String[] strArr) {
		if (strArr != null && strArr.length >= 1) {
			return 0;
		}
		return -1;
	}

	public static String getSpelling(int begin, int end) {
		return "";
	}

	public static int getTranslateIndexOf(int abbreviationIndex,String[] strArr) {
		for (int i = strArr.length - 1; i > abbreviationIndex; i--) {
			if (isChinese(strArr[i]) || isUpperString(strArr[i])) {
				// 有中文
				continue;
			} else {
				// 如果没有中文 判断下一个是不是
				if (i - 1> abbreviationIndex && (isChinese(strArr[i - 1]) || isUpperString(strArr[i - 1]))) {
					// 还有的话就继续
					continue;
				} else {
					return i + 1;
				}
			}
		}
		return -1;
	}

	public static String analysisTXT(File file) {
		StringBuilder result = new StringBuilder();
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
			while (true) {
				String str = br.readLine();
				if (str == null) {
					break;
				}
				String[] strArr = str.split(" ");
				String abbreviation = "";
				String spelling = "";
				String translate = "";
				int abbreviationIndex = -1;
				int translateIndex = -1;
				// 不符合规则的记录下来
				if (strArr.length < 2) {
					printError(str);
					continue;
				}
				// 判断是否存在缩写
				char firstCharInAbbreviation = strArr[0].charAt(0);
				char firstCharInSpelling = strArr[1].charAt(0);
				if ((firstCharInSpelling == '(' || isLowerCase(firstCharInSpelling))
						&& isUpperCase(firstCharInAbbreviation)) {
					// 说明存在缩写
					abbreviationIndex = getAbbreviationIndexOf(strArr);
					if (abbreviationIndex == -1) {
						printError(str);
						continue;
					}
					abbreviation = strArr[abbreviationIndex];
				}
				// 说明只有全拼和翻译 先找翻译
				translateIndex = getTranslateIndexOf(abbreviationIndex,strArr);
				if (translateIndex == -1) {
					System.err.println("2");
					printError(str);
					continue;
				} else {
					for (int i = translateIndex; i < strArr.length; i++) {
						if (i == strArr.length - 1) {
							translate += strArr[i].replaceAll(" ", "");
						} else {
							translate +=  strArr[i] + " ";
						}
					}
				}
				// 排错
				if (abbreviationIndex == translateIndex) {
					System.err.println("3");
					printError(str);
					continue;
				}
				// // 找到全拼
				for (int i = abbreviationIndex + 1; i < translateIndex; i++) {
					String tmp = strArr[i];
					if (i == abbreviationIndex + 1) {
						tmp = strArr[i];
						if (tmp.charAt(0) == '(') {
							tmp = tmp.substring(1);
						}
					}
					if (i == translateIndex - 1) {
						tmp = strArr[i];
						if (tmp.charAt(tmp.length()-1) == ')') {
							tmp = tmp.substring(0, tmp.length() - 2);
						}
//						tmp = strArr[i].replaceAll("[\\)]", "");
					} else {
						tmp += " ";
					}
					spelling += tmp;
				}
				printWord(str, abbreviation, spelling, translate);
			}

			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.toString();
	}

	public static void main(String[] args) {
//		File file = new File("D:/demo.txt");
//		String s = "asd abampere 电磁安培(等于 10A)";
//		String[] arr = s.split(" ");
		// System.out.println(getTranslateIndexOf(arr));
		// String[] strArr = new String[] { "www.micmiu.com",
		// "!@#$%^&*()_+{}[]|\"'?/:;<>,.", "！￥……（）——：；“”‘’《》，。？、", "不要啊)",
		// "やめて", "韩佳人", "???" };
		// for (String str : strArr) {
		// System.out.println("===========> 测试字符串：" + str);
		// System.out.println("正则判断结果：" + isChineseByREG(str) + " -- "
		// + isChineseByName(str));
		// System.out.println("Unicode判断结果 ：" + isChinese(str));
		// System.out.println("详细判断列表：");
		// char[] ch = str.toCharArray();
		// for (int i = 0; i < ch.length; i++) {
		// char c = ch[i];
		// System.out.println(c + " --> " + (isChinese(c) ? "是" : "否"));
		// }
		// }
//		analysisTXT(file);
	}

}